﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class mobile_hgconfirm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Server.UrlDecode(this.Request.Params["indname"].ToString());
        string recname = Server.UrlDecode(this.Request.Params["recname"].ToString());
        string productid = this.Request.Params["pid"].ToString();
        string recphone = this.Request.Params["recphone"].ToString();
        string pcode = this.Request.Params["pcode"].ToString();
        string add =Server.UrlDecode(this.Request.Params["add"].ToString());
        string confirm = this.Request.Params["confirm"].ToString();
        if (confirm.Equals("t"))
        {
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();
            SqlCommand get_rate = new SqlCommand();
            get_rate.Connection = sc1;
            get_rate.CommandText = "select EX_RATE from INFO_EXCHANGE where PRODUCT_ID=@idd";
            get_rate.Parameters.AddWithValue("@idd", productid);
            int rate = Convert.ToInt32(get_rate.ExecuteScalar().ToString());

           
            SqlCommand get_id = new SqlCommand();
            get_id.Connection = sc1;
            get_id.CommandText = "select IND_ID from INFO_IND where IND_NAME=@name";
            get_id.Parameters.AddWithValue("@name",name);
            string id = get_id.ExecuteScalar().ToString();

 
            SqlCommand get_credit = new SqlCommand("select IND_CREDITS from INFO_IND where IND_ID=@cid",sc1);
            get_credit.Parameters.AddWithValue("@cid",id);
            int i_credit =Convert.ToInt32(get_credit.ExecuteScalar().ToString());

            if (i_credit >= rate)
            {
                string dt = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
                SqlCommand confirm_com = new SqlCommand();
                confirm_com.CommandText = "insert into EXCHANGE_RECORD values (@id,@pid,@date,@add,@pcode,@name,@phone)";
                confirm_com.Connection = sc1;
                confirm_com.Parameters.AddWithValue("@id", id);
                confirm_com.Parameters.AddWithValue("@pid", productid);
                confirm_com.Parameters.AddWithValue("@date", dt);
                confirm_com.Parameters.AddWithValue("@add", add);
                confirm_com.Parameters.AddWithValue("@pcode", pcode);
                confirm_com.Parameters.AddWithValue("@name", recname);
                confirm_com.Parameters.AddWithValue("@phone", recphone);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁     
                if (confirm_com.ExecuteNonQuery() == 1)
                {
                    Application["sqllock"] = "unlock";//解锁
                    //扣除积分数，sum-1


                    SqlCommand updatecre = new SqlCommand();
                    updatecre.Connection = sc1;
                    updatecre.CommandText = "update INFO_IND set IND_CREDITS=IND_CREDITS-@rate where IND_ID=@iddd";
                    updatecre.Parameters.AddWithValue("@rate", rate);
                    updatecre.Parameters.AddWithValue("@iddd", id);
                    while (Application["sqllock"] == "lock")
                    {//检测是否已经锁住
                        System.Threading.Thread.Sleep(2000);
                    }
                    Application["sqllock"] = "lock";//上锁 
                    try
                    {
                        updatecre.ExecuteNonQuery();
                    }
                    finally
                    {
                        Application["sqllock"] = "unlock";//解锁
                    }

                    SqlCommand updatesum = new SqlCommand();
                    updatesum.Connection = sc1;
                    updatesum.CommandText = "update INFO_EXCHANGE set EX_SUM=EX_SUM-1 where PRODUCT_ID=@idddd";
                    updatesum.Parameters.AddWithValue("@idddd", productid);
                    while (Application["sqllock"] == "lock")
                    {//检测是否已经锁住
                        System.Threading.Thread.Sleep(2000);
                    }
                    Application["sqllock"] = "lock";//上锁
                    try
                    {
                        updatesum.ExecuteNonQuery();
                    }
                    finally
                    {
                        Application["sqllock"] = "unlock";//解锁
                    }

                    sc1.Close();
                    Page.RegisterStartupScript("ss", "<script>alert('确认成功，我们即将发货!')</script>");
                    return;
                }
                else
                {
                    Application["sqllock"] = "unlock";//解锁
                    sc1.Close();
                    Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请尝试重新确认!')</script>");
                    return;
                }
            }
            else
            {
                Page.RegisterStartupScript("ss", "<script>alert('您的积分在确认前发生了变动，请尝试重新确认!')</script>");
                return;
            }
           
        }
        else
        {
            Page.RegisterStartupScript("ss", "<script>alert('您无权访问这个页面!')</script>");
            return;
        }
    }
}